package cn.com.server.common.util;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class SigarUtil {
	
	public static Sigar sigar = new Sigar();

	private static Map<String,Object> o;
	
	
	public static Map<String,Object> getFileSystem() {
		Map<String,Object> fs = new HashMap<String, Object>();
//		Sigar sigar = null;
		try {
			float total = 0;
			float used = 0;
			float avail = 0;
			FileSystem[] fileSystemArray = sigar.getFileSystemList();
			for (FileSystem fileSystem : fileSystemArray) {
//				if(fileSystem.getType()==2) {
					FileSystemUsage fileSystemUsage = null;
					try {
						fileSystemUsage = sigar.getFileSystemUsage(fileSystem.getDirName());
						//sigar.getFileSystemUsage(fileSystem.getDirName());
					} catch (SigarException e) {// 当fileSystem.getType()为5时会出现该异常——此时文件系统类型为光驱
						e.printStackTrace();
						continue;
					}
					total = total + fileSystemUsage.getAvail() + fileSystemUsage.getUsed();
					avail = avail + fileSystemUsage.getAvail();
					//total + fileSystemUsage.getTotal();
					used = used + fileSystemUsage.getUsed();
//					System.out.println("fileSystemUsage.getFree()-->"+fileSystemUsage.getFree());
//					System.out.println("fileSystemUsage.getUsed()-->"+fileSystemUsage.getUsed());
//					System.out.println("fileSystemUsage.getTotal()-->"+fileSystemUsage.getTotal());
//					System.out.println("fileSystemUsage.getFreeFiles()-->"+fileSystemUsage.getFreeFiles());
//					System.out.println("fileSystemUsage.getFreeFiles()-->"+fileSystemUsage.getAvail());
//				}
			}
			CpuPerc[] cpus = sigar.getCpuPercList();
			CpuInfo[] cpuInfos = sigar.getCpuInfoList();
			double cpu = 0;
			for(CpuPerc cp:cpus){
				cpu = cpu + cp.getUser();
			}
			String uu = "K";
			String tu = "K";
			String au = "K";
			
			double totalu = total;
			double usedu = used;
			double availu = avail;
			
			if((totalu)>1024d) {
				totalu = totalu/1024d;
				tu = "M";
			}
			if((totalu)>1024d) {
				totalu = totalu/1024d;
				tu = "G";
			}
			if((totalu)>1024d) {
				totalu = totalu/1024d;
				tu = "T";
			}
			if((usedu)>1024d) {
				usedu = usedu/1024d;
				uu = "M";
			}
			if((usedu)>1024d) {
				usedu = usedu/1024d;
				uu = "G";
			}
			if((usedu)>1024d) {
				usedu = usedu/1024d;
				uu = "T";
			}
			if((availu)>1024d) {
				availu = availu/1024d;
				au = "M";
			}
			if((availu)>1024d) {
				availu = availu/1024d;
				au = "G";
			}
			if((availu)>1024d) {
				availu = availu/1024d;
				au = "T";
			}
			fs.put("sdavailable",new BigDecimal(availu).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+au);
			fs.put("sdused",new BigDecimal(usedu).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+uu);
			fs.put("sdtotal",new BigDecimal(totalu).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+tu);
			fs.put("sd",new BigDecimal(used/total*100).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
			if(Double.isNaN(cpu)) {
				cpu = ((double) o.get("cpu")/100)*cpus.length;
			}
			
			fs.put("cpuavailable",new BigDecimal(cpuInfos[0].getMhz()/1000d -(cpuInfos[0].getMhz()*cpu/cpus.length)/1000d).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"GHz");
			fs.put("cputotal",new BigDecimal(cpuInfos[0].getMhz()/1000d).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"GHz");
			fs.put("cpuused",new BigDecimal((cpuInfos[0].getMhz()*cpu/cpus.length)/1000d).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"GHz");
			fs.put("cpu",new BigDecimal(cpu/cpus.length*100).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
			Mem mem = sigar.getMem();  
			float memUsed = mem.getUsed();
			float memTotal = mem.getTotal();
			float memAvail = memTotal-memUsed;
			
			String um = "B";
			String tm = "B";
			String am = "B";
			
			double memUsedu = memUsed;
			double memTotalu = memTotal;
			double memAvailu = memAvail;
			
			if((memTotalu)>1024d) {
				memTotalu = memTotalu/1024d;
				tm = "K";
			}
			
			if((memTotalu)>1024d) {
				memTotalu = memTotalu/1024d;
				tm = "M";
			}
			if((memTotalu)>1024d) {
				memTotalu = memTotalu/1024d;
				tm = "G";
			}
			if((memTotalu)>1024d) {
				memTotalu = memTotalu/1024d;
				tm = "T";
			}
			if((memUsedu)>1024d) {
				memUsedu = memUsedu/1024d;
				um = "K";
			}
			if((memUsedu)>1024d) {
				memUsedu = memUsedu/1024d;
				um = "M";
			}
			if((memUsedu)>1024d) {
				memUsedu = memUsedu/1024d;
				um = "G";
			}
			if((memUsedu)>1024d) {
				memUsedu = memUsedu/1024d;
				um = "T";
			}
			if((memAvailu)>1024d) {
				memAvailu = memAvailu/1024d;
				am = "K";
			}
			if((memAvailu)>1024d) {
				memAvailu = memAvailu/1024d;
				am = "M";
			}
			if((memAvailu)>1024d) {
				memAvailu = memAvailu/1024d;
				am = "G";
			}
			if((memAvailu)>1024d) {
				memAvailu = memAvailu/1024d;
				am = "T";
			}
			fs.put("memAvailu",new BigDecimal(memAvailu).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+am);
			fs.put("memUsedused",new BigDecimal(memUsedu).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+um);
			fs.put("memUsedtotal",new BigDecimal(memTotalu).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+tm);
			fs.put("mem",new BigDecimal(memUsed/memTotal*100).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
			o = fs;
		} catch (Exception e) {
			e.printStackTrace();
		} 
//		finally {
//			if (sigar != null)
//				sigar.close();
//		}
		return o;
	}
}
